package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.service.YddUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright (C), 2021-2022
 * Author: 16372
 * Date: 2022/3/3 12:38
 * FileName: YddUserController
 * Description: 会员用户接口
 */
@RestController
@RequestMapping("/ydd_user")
@Slf4j
public class YddUserController {
    @Autowired
    private YddUserService yddUserService;

    @GetMapping("/rowInfo")
    public ResultData rowInfo(String id){
        YddUser user = yddUserService.getById(id);
        return ResultData.ok("",user);
    }

    @GetMapping("/list")
    public ResultData list(Integer sex,String username,String mobile,Integer page,Integer limit){
        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != sex,YddUser::getSex,sex);
        queryWrapper.eq(null != username,YddUser::getUsername,username);
        queryWrapper.eq(null != mobile,YddUser::getMobile,mobile);
        Page<YddUser> userPage = new Page<>(page, limit);
        yddUserService.page(userPage,queryWrapper);
        return ResultData.ok("",PageResult.init(userPage));
    }

    @PostMapping("/add")
    public ResultData add(@RequestBody YddUser user){
        yddUserService.save(user);
        return ResultData.ok("","");
    }

    @GetMapping("/del")
    public ResultData del(String id){
        yddUserService.removeById(id);
        return ResultData.ok("","");
    }

    @PostMapping("/update")
    public ResultData update(@RequestBody YddUser user){
        yddUserService.updateById(user);
        return ResultData.ok("","");
    }

}
